package kuzha.runtime.jadapter;

import java.util.AbstractList;
import java.util.List;

import kuzha.KuzhaObject;
import kuzha.Utils;
import kuzha.runtime.Converter;

public class FromListAdapter<T> extends AbstractList<KuzhaObject> implements FromJavaObjectAdapter {
	private final List<T> list;
	private final Class<T> cls;
	
	public FromListAdapter(List<T> list, Class<T> cls) {
		if (list == null || cls == null)
			throw new IllegalArgumentException();
		this.list = list;
		this.cls = cls;
	}
	
	@Override
	public Object toJavaObject() {
		return list;
	}
	
	@Override
	public String toString() {
		return "[" + Utils.join(" ", list.toArray()) + "]";
	}
	
	@Override
	public void add(int index, KuzhaObject element) {
		list.add(index, Converter.cast(cls, element));
	}
	
	@Override
	public KuzhaObject get(int arg0) {
		return Converter.toKuzhaObject(list.get(arg0));
	}
	
	@Override
	public KuzhaObject remove(int index) {
		return Converter.toKuzhaObject(list.remove(index));
	}
	
	@Override
	public KuzhaObject set(int index, KuzhaObject element) {
		return Converter.toKuzhaObject(list.set(index, Converter.cast(cls, element)));
	}
	
	@Override
	public int size() {
		return list.size();
	}
}
